package com.hzhjhh.view.cashierView;

import java.lang.reflect.Field;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.collections4.CollectionUtils;

import com.hzhjhh.entity.Merchandise;
import com.hzhjhh.view.cashierView.enums.MerchandiseShowEnum;

public class MerchandiseShowTableModel extends AbstractTableModel {
	
	private List<Merchandise> merchandiseList ;
	private MerchandiseShowEnum[] showEnums = MerchandiseShowEnum.values();
	
	

	public MerchandiseShowTableModel(List<Merchandise> merchandiseList) {
		if (CollectionUtils.isEmpty(merchandiseList)) {
			return;
		}
		this.merchandiseList = merchandiseList;
	}

	@Override
	public int getRowCount() {
		return CollectionUtils.size(merchandiseList);
	}

	@Override
	public int getColumnCount() {
		return CollectionUtils.size(showEnums);
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Merchandise merchandise = CollectionUtils.get(merchandiseList, rowIndex);
		String fieldName = showEnums[columnIndex].getCloumnName();
		Object object = null;
		try {
			Field field = merchandise.getClass().getDeclaredField(fieldName);
			field.setAccessible(true);
			object = field.get(merchandise);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return object;
	}

}
